﻿using ReflectionStudio.Core.Events;
using System;
using System.Collections;
using System.Data;
using WPFExcelReport.Helpers;
using System.Windows;
namespace WPFExcelReport
{
    public partial class ITXEXRPT
    {
        public void YGRVKPOS()
        {
            string msg = "";
            string sdkey = "";
            try
            {
                if (!dsSource.Tables.Contains("VAART"))
                {
                    return;
                }
                if (!dsSource.Tables.Contains("VAFARBEN"))
                {
                    return;
                }
                string src = "";
                string sgb = "";
                DataTable tbVKKOPF = dsSource.Tables["VKKOPF"];
                DataTable tbVKPOS = dsSource.Tables["VKPOS"];
                DataRow dwVKPOS1 = tbVKPOS.Rows[0];
                string KRS;
                string LFD;
                string POS;
                string TXTISIND;
                string str1 = "", str2 = "";
                int position = 0;
                DataRow dwVKK = tbVKKOPF.Rows[0];
                DataTable tbVKPBEST = new DataTable(), tbVKK07 = new DataTable();
                DataTable TTGAAUF = new DataTable();
                string VKPMEEH = "";
                DataRow dwVKP = tbVKPOS.Rows[0]; 
       
                DataTable tbVA07 = dsSource.Tables["VAART"];
                DataTable tbVA10 = dsSource.Tables["VAFARBEN"];
                DataRow dwVA07 = null; 
                double VKKQTY = 0;
                string order = "";
                string currency = dwVKK["VKKHWAEHRSL"].ToString();
                VKPMEEH = dwVKP["VKPMEEH"].ToString();
                string VKPPREH = dwVKP["VKPPREH"].ToString();
                string VKPZUDOPA="0";
                string package = "";
                #region=============Header==================
               double USD2RMB=Convert.ToDouble(dwVKK["VKKKURS"].ToString())*0.0001;
                //src = "确认号码/Proforma Invoice：" + dwVKK["VKKAUFKRS"].ToString() + "-" + System.Convert.ToInt64(dwVKK["VKKAUFLFD"].ToString());
               order = dwVKK["VKKAUFKRS"].ToString() + "-" + Helper.GetSubRollNum(dwVKK["VKKAUFLFD"].ToString());
               exAgent.SetCellValue(2, 32, order, true);
               exAgent.RenameSheet(order);
                exAgent.SetCellValue(6, 32, date);
                src = dwVKK["VKKBESTNR"].ToString();
                exAgent.SetCellValue(4, 8, src);
                src = dwVKK["VKKBESTDAT"].ToString();
                exAgent.SetCellValue(5, 8, src);
                DataRow dwHR = null;
                var qpr = from DataRow row in dsSource.Tables["HRSTAMM"].AsEnumerable()
                          where row["HRBENID"].ToString().Trim() == dwVKK["VKKSACHBE"].ToString().Trim()
                          select row;
                foreach (DataRow row in qpr)
                {
                    dwHR = row;
                    exAgent.SetCellValue(4, 32, GetGB2312Value(dwHR["HRNNAME"].ToString().Substring(0, 6)));
                    exAgent.SetCellValue(5, 32,dwHR["HRPTEL"].ToString().Trim());
                }
                
                #endregion

                //exAgent.SetCellValue(x+4, 1, "Terms of payment:");
                src = dwVKK["VKKZAHLBED"].ToString();//付款方式
                var qz = from dwTemp in dsSource.Tables["ICC_TXTKON"].AsEnumerable()
                         where dwTemp["TXTIPIND"].ToString().Trim() == src.Trim() + "V"
                         select dwTemp;
                foreach (DataRow row in qz)
                {
                    exAgent.SetCellValue(128, 7, row["TXT010"].ToString());
                }

                src = dwVKK["VKKVERSART"].ToString();//运输方式Truck  
                //exAgent.SetCellValue(131, 5, Helper.GetSDDAT(dsSource, "0208", src, "V").Trim());           
                //交易类型FOB
                src = dwVKK["VKKLIEFBED"].ToString();//运输方式Truck
                //exAgent.SetCellValue(142, 4, Helper.GetSDDAT(dsSource, "0241", src, "V").Trim());   

                int xStart = 13;
                int rown = 2;
                int idx = 0;
                string finish = "";
                double TAmount = 0;
                int count = tbVKPOS.Rows.Count;
                foreach (DataRow dwVKPOS in tbVKPOS.Rows)
                {
                    string color = "";
                    dwVA07 = null;
                    msg = "开始写入：" + idx+1 + "行";
                    EventDispatcher.Instance.RaiseStatus(msg, StatusEventType.StopProgress);
                    POS = dwVKPOS["VKPAUFPOS"].ToString();
                    exAgent.SetCellValue(xStart + idx * rown, 1, Helper.GetSubRollNum(POS));

                    #region====================VKPOS=======================
                    exAgent.SetCellValue(xStart + idx * rown, 2, dwVKPOS["VKPART"].ToString().Trim() + Environment.NewLine + GetGB2312Value(dwVKPOS["VKPKDARTNR"].ToString().TrimEnd()));
                    exAgent.SetCellValue(xStart + idx * rown, 8, dwVKPOS["VKPFARBE"].ToString().Trim() + Environment.NewLine + dwVKPOS["VKPKDFARBNR"].ToString().TrimEnd());
                    #endregion
                    #region================成份===================
                    var qVA07 = from DataRow row in tbVA07.AsEnumerable()
                                where row["VAAART"].ToString() == dwVKPOS["VKPART"].ToString()
                                select row;
                    foreach (DataRow row in qVA07)
                    {
                        dwVA07 = row;
                    }
                    if (dwVA07 != null)
                    {
                        src = Helpers.Helper.GetCompositionVA(dsSource, dwVA07,"V");
                        exAgent.SetCellValue(xStart + idx * rown + 1, 2, "成份:" + GetGB2312Value(src));
                    }
                    #endregion
                    #region================客人花号/品名======================
                    src = dwVKPOS["VKPKDARTNR"].ToString().TrimEnd() + " " + dwVKPOS["VKPKDBEZ"].ToString().TrimEnd() + " " + dwVKPOS["VKPKDFARBNR"].ToString().TrimEnd();
                    var qvakd = from DataRow row in dsSource.Tables["VAKDBEZ"].AsEnumerable()
                                where row["VAKBKOLL"].ToString() == dwVKPOS["VKPKOLL"].ToString()
                                && row["VAKBART"].ToString() == dwVKPOS["VKPART"].ToString()
                                && row["VAKBFARBE"].ToString() == dwVKPOS["VKPFARBE"].ToString()
                                select row;
                    foreach (DataRow rkd in qvakd)
                    {
                        src = rkd["VAKBKDARTNR"].ToString().TrimEnd() + " " + rkd["VAKBKDARTBEZ"].ToString().TrimEnd() + " " + rkd["VAKBKDFARBNR"].ToString().TrimEnd();
                    }
                    //exAgent.SetCellValue(xStart + idx * rown + 1, 2, GetGB2312Value(src));
                    #endregion
                    #region================数量===================
                    exAgent.SetCellValue(12, 18, "数量" + Environment.NewLine + "(" + VKPMEEH + ")");
                    src = dwVKPOS["VKPMENGE1"].ToString();
                    string VKPMENGE1 = Divide(src, 100);
                    VKKQTY += System.Convert.ToDouble(VKPMENGE1);
                    exAgent.SetCellValue(xStart + idx * rown, 16, VKPMENGE1);

                    exAgent.SetCellValue(12, 20, "单价" + Environment.NewLine + " (" + currency + "/" + VKPPREH + ")");
                    src = dwVKPOS["VKPPREIS"].ToString();
                    exAgent.SetCellValue(xStart + idx * rown, 20, Divide(src, 1000));

                    double pvalue = System.Math.Round(Convert.ToDouble(dwVKPOS["VKPPOSWERT"].ToString()) / USD2RMB, 2);
                    TAmount += System.Convert.ToDouble(pvalue);
                    exAgent.SetCellValue(xStart + idx * rown, 25, pvalue);

                    exAgent.SetCellValue(12, 28, "金额(" + currency + ")"); 
                    #endregion
                    #region =================交期======================
                    exAgent.SetCellValue(xStart + idx * rown, 12, dwVKPOS["VKPLIEFTERM"].ToString());
                    //exAgent.SetCellValue(xStart + idx * rown + 1, 28,"确认:"+ dwVKPOS["VKPLIEFTERM"].ToString(), true);
                    #endregion
                    #region =================包装======================
                    if (package == "")
                    {
                        src = dwVKPOS["VKPVERPART"].ToString();//运输方式Truck
                        package = Helper.GetSDDAT(dsSource, "0209", src, "V").Trim();
                        exAgent.SetCellValue(126, 7, package);
                    }
                    #endregion
                    #region =================溢短装======================
                    if (VKPZUDOPA == "0")
                    {
                        VKPZUDOPA = dwVKPOS["VKPZUDOPA"].ToString();
                    }
                    exAgent.SetCellValue(127, 7, "按合同备注项要求验收，重量允许有"+VKPZUDOPA+"偏差；");
                    //exAgent.SetCellValue(xStart + idx * rown + 1, 28,"确认:"+ dwVKPOS["VKPLIEFTERM"].ToString(), true);
                    #endregion
                    
                    #region =================标准====================

                    string std = GetGB2312Value(dwVKPOS["COLORSTD"].ToString().Trim());
                    exAgent.SetCellValue(xStart + idx * rown, 32, std);
                     #endregion
                    idx++;
                    if (idx < count)
                    {
                        exAgent.RangeCopy(13, 14, xStart + idx * rown, xStart + idx * rown + 1);
                    }

                }
                exAgent.SetCellValue(114, 12, VKKQTY, true);
                exAgent.SetCellValue(114, 20, TAmount, true);
                FillFooter(135, dwVKK);
            }
            catch (Exception ex)
            {
                msg = "有错误产生，请注意:";
                EventDispatcher.Instance.RaiseStatus(msg, StatusEventType.StopProgress);
                Tracer.Error(ex.StackTrace, ex); 
            }
            finally
            {
                msg = "指令表虽然已经生产，但请注意核对。";
                EventDispatcher.Instance.RaiseStatus(msg, StatusEventType.StopProgress);
            }
        }
        public void FillFooter(int x, DataRow dwVKK)
        {
            string src = "";
            DataRow dwKD03 = null;
            var qkd = from dwTemp in dsSource.Tables["KDSTAMM"].AsEnumerable()
                      where dwTemp["KDKUNDNR"].ToString() == dwVKK["VKKKUNDNR"].ToString()
                      select dwTemp;
            foreach (DataRow row in qkd)
            {
                dwKD03 = row;
            }
            if (dwKD03 != null)
            {
                //   src = "客户编号/Client No:";
                //exAgent.SetCellValue(8, 30, src); 
                string kdnr = dwKD03["KDKUNDNR"].ToString();
                //src = dwKD03["KDKNAME"].ToString();
                //exAgent.SetCellValue(4, 7, Helper.GetGB2312Value(src).Trim());
                string kdname = dwKD03["KDNAME1"].ToString();
                exAgent.SetCellValue(3, 1, "需方:" + Helper.GetGB2312Value(kdname).Trim() + "(客户编号:" +Helper.GetSubRollNum(kdnr) + ")");
                exAgent.SetCellValue(135, 19, Helper.GetGB2312Value(kdname).Trim());
                src = dwKD03["KDSTRASSE1"].ToString();
                exAgent.SetCellValue(137, 21, Helper.GetGB2312Value(src).Trim());
                src = dwKD03["KDTEL"].ToString();
                exAgent.SetCellValue(142, 21, Helper.GetGB2312Value(src).Trim());
                src = dwKD03["KDFAX"].ToString();
                exAgent.SetCellValue(143, 21, Helper.GetGB2312Value(src).Trim());
                /* src = dwKD03["KDMAILADR"].ToString();
               exAgent.SetCellValue(10, 1, Helper.GetGB2312Value(src).Trim()); */
            }
              qkd = from dwTemp in dsSource.Tables["KDANSP"].AsEnumerable()
                    where dwTemp["KDSANSPNA"].ToString() == dwVKK["VKKANSPRECH"].ToString()
                      select dwTemp;
              foreach (DataRow dwKD13 in qkd)
              { 
                  //   src = "客户编号/Client No:";
                  //exAgent.SetCellValue(8, 30, src); 
                  string name =Helper.Convert8859P1ToGB2312(dwKD13["KDSANSPNAME"].ToString()).Trim();
                  string phone = dwKD13["KDSTEL"].ToString().Trim(); 
                  exAgent.SetCellValue(118, 9, name+":"+phone); 
              } 
        }
    }
}
